package com.wbdgj.ui.branch;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.ChoiceBranchAdapter;
import com.wbdgj.adapter.FootprintBranchAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.booking.BookingActivity;
import com.wbdgj.ui.home.WbDetailActivity;
import com.wbdgj.ui.store.StoreCofeActivity;
import com.wbdgj.ui.store.StoreHotelActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

public class FootprintBranchActivity extends BaseActivity {

    private Context context = this;
    private String branchId;

    @BindView(R.id.demo_listview)
    PullListView demo_listview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    FootprintBranchAdapter choiceBranchAdapter;

    @BindView(R.id.text_top_right)
    TextView text_top_right;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_choice_branch;
    }

    @Override
    protected void initView() {
        text_top_right.setText("我的足迹");
        findViewById(R.id.searchLay).setVisibility(View.GONE);
        upbranchlist();
        demo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!choiceBranchAdapter.hasNoData){
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_ID, decimalFormat.format(choiceBranchAdapter.getItem(i).get("branch_id")));
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_NAME, choiceBranchAdapter.getItem(i).get("branch_name")+"");

                    if(choiceBranchAdapter.getItem(i).containsKey("branch_attributes")){
                        if(decimalFormat.format(choiceBranchAdapter.getItem(i).get("branch_attributes")).equals("0")){
                            startActivity(new Intent(context, StoreCofeActivity.class));
                        }else if(decimalFormat.format(choiceBranchAdapter.getItem(i).get("branch_attributes")).equals("1")){
                            startActivity(new Intent(context, BookingActivity.class));
                        }else{
                            startActivity(new Intent(context, StoreHotelActivity.class));
                        }
                    }else{
                        startActivity(new Intent(context, StoreCofeActivity.class));
                    }

                }
            }
        });

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        upbranchlist();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
    }

    private void upbranchlist() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().upbranchlist(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    List<LinkedTreeMap> linkedTreeMaps  = (List<LinkedTreeMap>) linkedTreeMap.get("data");
                    choiceBranchAdapter = new FootprintBranchAdapter(context, linkedTreeMaps);
                    demo_listview.setAdapter(choiceBranchAdapter);
                }
            }
        });
    }
}
